<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>webGL|webgl2.0工作原理[转] | 一只咸鱼 | 努力学习最新姿势！！！</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="WebGL">
    <meta name="description" content="WebGL2 工作原理看了这个文章会让你更知道怎么写 WebGL 代码而已。  原理细节远远不够， 更多的细节应该是看Opengl ，图形渲染管线， CPU与GPU通信， GPU流水线 。  WebGL 工作原理我们需要探讨一下WebGL在GPU上究竟做了什么。WebGL在GPU上的工作基本上分为两部分，第一部分是将顶点（或数据流）转换到裁剪空间坐标， 第二部分是基于第一部分的结果绘制像素点。 当">
<meta property="og:type" content="article">
<meta property="og:title" content="webGL|webgl2.0工作原理[转]">
<meta property="og:url" content="http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/index.html">
<meta property="og:site_name" content="一只咸鱼">
<meta property="og:description" content="WebGL2 工作原理看了这个文章会让你更知道怎么写 WebGL 代码而已。  原理细节远远不够， 更多的细节应该是看Opengl ，图形渲染管线， CPU与GPU通信， GPU流水线 。  WebGL 工作原理我们需要探讨一下WebGL在GPU上究竟做了什么。WebGL在GPU上的工作基本上分为两部分，第一部分是将顶点（或数据流）转换到裁剪空间坐标， 第二部分是基于第一部分的结果绘制像素点。 当">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-27T04:36:00.000Z">
<meta property="article:modified_time" content="2020-04-27T04:50:46.480Z">
<meta property="article:author" content="Mr.Zhang">
<meta property="article:tag" content="WebGL">
<meta name="twitter:card" content="summary">
    
        <link rel="alternate" type="application/atom+xml" title="一只咸鱼" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Mr.Zhang</h5>
          <a href="mailto:zhangticcc@163.com" title="zhangticcc@163.com" class="mail">zhangticcc@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                首页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                时间轴
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://weibo.com/u/5392412243" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                微博
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/zhangti0708" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                开源地址
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://blog.csdn.net/weixin_40902527" target="_blank" >
                <i class="icon icon-lg icon-rss"></i>
                技术博客
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://zhangticcc.gitee.io/webgis/" target="_blank" >
                <i class="icon icon-lg icon-globe"></i>
                GIS案例
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://mp.weixin.qq.com/s/p4TOhKcr4RZkZWzuMYyBEQ" target="_blank" >
                <i class="icon icon-lg icon-weixin"></i>
                微信订阅号
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">webGL|webgl2.0工作原理[转]</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">webGL|webgl2.0工作原理[转]</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-04-27T04:36:00.000Z" itemprop="datePublished" class="page-time">
  2020-04-27
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/%E9%9A%8F%E7%AC%94/">随笔</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#WebGL2-工作原理"><span class="post-toc-number">1.</span> <span class="post-toc-text">WebGL2 工作原理</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#WebGL-工作原理"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">WebGL 工作原理</span></a></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-webGL-webgl2-0工作原理-转"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">webGL|webgl2.0工作原理[转]</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-04-27 12:36:00" datetime="2020-04-27T04:36:00.000Z"  itemprop="datePublished">2020-04-27</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/%E9%9A%8F%E7%AC%94/">随笔</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h4 id="WebGL2-工作原理"><a href="#WebGL2-工作原理" class="headerlink" title="WebGL2 工作原理"></a>WebGL2 工作原理</h4><p>看了这个文章会让你更知道怎么写 WebGL 代码而已。  原理细节远远不够， 更多的细节应该是看Opengl ，图形渲染管线， CPU与GPU通信， GPU流水线 。 </p>
<h5 id="WebGL-工作原理"><a href="#WebGL-工作原理" class="headerlink" title="WebGL 工作原理"></a>WebGL 工作原理</h5><p>我们需要探讨一下WebGL在GPU上究竟做了什么。WebGL在GPU上的工作基本上分为两部分，第一部分是将顶点（或数据流）转换到裁剪空间坐标， 第二部分是基于第一部分的结果绘制像素点。</p>
<p>当你调用</p>
<p><code>gl.drawArrays(gl.TRIANGLE, 0, 9);</code><br>这里的9表示“处理9个顶点”，所以将会有9个顶点被转换。</p>
<p>左侧是你提供的数据。顶点着色器（Vertex Shader）是你写进GLSL 中的一个方法，每个顶点调用一次，在这个方法中做一些数学运算后设置了一个特殊的gl_Position变量， 这个变量就是该顶点转换到裁剪空间中的坐标值，GPU接收该值并将其保存起来。</p>
<p>假设你正在画三角形，顶点着色器每完成三次顶点处理，WebGL就会用这三个顶点画一个三角形。它计算出这三个顶点对应的像素后，就会光栅化这个三角形，“光栅化”其实就是“用像素画出来” 的花哨叫法。对于每一个像素，它会调用你的片断着色器询问你使用什么颜色。你通过给片断着色器的一个特殊变量gl_FragColor设置一个颜色值，实现自定义像素颜色。</p>
<p>使用它们可以做出非常有趣的东西，但如你所见，到目前为止的例子中， 处理每个像素时片断着色器可用信息很少，幸运的是我们可以给它传递更多信息。想要从顶点着色器传值到片断着色器，我们可以定义“可变量（varyings）”。</p>
<p>一个简单的例子，将顶点着色器计算出的裁剪空间坐标从顶点着色器传递到片断着色器。</p>
<p>我们来画一个简单的三角形。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 定义一个三角形填充到缓冲里</span><br><span class="line">function setGeometry(gl) &#123;</span><br><span class="line">gl.bufferData(</span><br><span class="line">gl.ARRAY_BUFFER,</span><br><span class="line">new Float32Array([</span><br><span class="line">0, -100,</span><br><span class="line">150, 125,</span><br><span class="line">-175, 100]),</span><br><span class="line">gl.STATIC_DRAW);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们只需要画三个顶点。</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; Draw the scene.</span><br><span class="line">function drawScene() &#123;</span><br><span class="line">...</span><br><span class="line">&#x2F;&#x2F; Draw the geometry.</span><br><span class="line">gl.drawArrays(gl.TRIANGLES, 0, 3);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后在我们的顶点着色器中定义一个varying（可变量）用来给片断着色器传值。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">varying vec4 v_color;</span><br><span class="line">...</span><br><span class="line">void main() &#123;</span><br><span class="line">&#x2F;&#x2F; 将位置和矩阵相乘</span><br><span class="line">gl_Position &#x3D; vec4((u_matrix * vec3(a_position, 1)).xy, 0, 1);</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; 从裁减空间转换到颜色空间</span><br><span class="line">&#x2F;&#x2F; 裁减空间范围 -1.0 到 +1.0</span><br><span class="line">&#x2F;&#x2F; 颜色空间范围 0.0 到 1.0</span><br><span class="line">v_color &#x3D; gl_Position * 0.5 + 0.5;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在片断着色器中定义同名varying变量。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#version 300 es</span><br><span class="line">precision mediump float;</span><br><span class="line">in vec4 v_color;</span><br><span class="line">out vec4 outColor;</span><br><span class="line"> </span><br><span class="line">void main() &#123;</span><br><span class="line">outColor &#x3D; v_color;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>WebGL会将同名的可变量从顶点着色器输入到片断着色器中。</p>
<p>下面是运行结果。</p>
<p><a href="https://webglfundamentals.org/webgl/webgl-2d-triangle-with-position-for-color.html" target="_blank" rel="nofollow noopener" data-token="c46449d6e20dbd15ea06d7e7b5a2dfe6" style="box-sizing: border-box;outline: 0px;color: rgb(103, 149, 181);cursor: pointer;overflow-wrap: break-word;" data-linktype="2">点此在新窗口中浏览</a></p>
<p>当你移动，缩放，旋转三角形时，发现颜色随位置变化，不跟着三角形移动。</p>
<p>回想一下，我们只计算了三个顶点，调用了三次顶点着色器，所以也只计算出了三个颜色值， 但是我们的三角形却有很多颜色，这就是称之为可变量的varying的原因啦！</p>
<p>WebGL先获得顶点着色器中计算的三个颜色值，在光栅化三角形时将会根据这三个值进行插值。每一个像素在调用片断着色器时，可变量的值是与之对应的插值。</p>
<p>让我们从上例的三个顶点开始分析</p>
<table cellpadding="0" cellspacing="0" width="851" style="width: 654px;"><tbody style="box-sizing: border-box;outline: 0px;border-width: 0px;border-style: initial;border-color: initial;overflow-wrap: break-word;"><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);overflow-wrap: break-word;"><td colspan="2" style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">顶点</p></td></tr><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);background-color: rgb(247, 247, 247);overflow-wrap: break-word;"><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">-100</p></td></tr><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);overflow-wrap: break-word;"><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">150</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">125</p></td></tr><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);background-color: rgb(247, 247, 247);overflow-wrap: break-word;"><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">-175</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">100</p></td></tr></tbody></table>
我们的给顶点着色器施加了一个包含平移，旋转和缩放的的矩阵，并将结果转换到裁剪空间。默认平移，旋转和缩放值为：平移 = 200, 150，旋转 = 0，缩放 = 1，所以这里只进行了平移。画布大小（背景缓冲）为 400×300，所以三个顶点在裁剪空间中为以下坐标值。
<table cellpadding="0" cellspacing="0" width="851" style="width: 654px;"><tbody style="box-sizing: border-box;outline: 0px;border-width: 0px;border-style: initial;border-color: initial;overflow-wrap: break-word;"><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);overflow-wrap: break-word;"><td colspan="3" style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">写入 gl_Position 的值</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><br></td></tr><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);background-color: rgb(247, 247, 247);overflow-wrap: break-word;"><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.000</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.660</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><br></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><br></td></tr><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);overflow-wrap: break-word;"><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.750</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">-0.830</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><br></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><br></td></tr><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);background-color: rgb(247, 247, 247);overflow-wrap: break-word;"><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">-0.875</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">-0.660</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><br></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><br></td></tr></tbody></table>

<p>同时将这些值转换到颜色空间中赋给我们定义的可变量v_color。</p>
<table cellpadding="0" cellspacing="0" width="851" style="width: 654px;"><tbody style="box-sizing: border-box;outline: 0px;border-width: 0px;border-style: initial;border-color: initial;overflow-wrap: break-word;"><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);overflow-wrap: break-word;"><td colspan="3" style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">写入 v_color 的值</p></td></tr><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);background-color: rgb(247, 247, 247);overflow-wrap: break-word;"><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.5000</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.830</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.5</p></td></tr><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);overflow-wrap: break-word;"><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.8750</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.086</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.5</p></td></tr><tr style="box-sizing: border-box;outline: 0px;border-width: 1px 0px 0px;border-right-style: initial;border-bottom-style: initial;border-left-style: initial;border-right-color: initial;border-bottom-color: initial;border-left-color: initial;border-top-style: solid;border-top-color: rgb(221, 221, 221);background-color: rgb(247, 247, 247);overflow-wrap: break-word;"><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.0625</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.170</p></td><td style="box-sizing: border-box;outline: 0px;padding: 8px;overflow-wrap: break-word;color: rgb(79, 79, 79);line-height: 22px;"><p style="box-sizing: border-box;outline: 0px;font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif;line-height: 22px;overflow-x: auto;overflow-wrap: break-word;">0.5</p></td></tr></tbody></table>
利用这三个值进行插值后传进每个像素运行的片断着色器中。







<p><code>v_color</code>在v0，v1和v2之间插值</p>
<p>想要给片断着色器传值，我们可以先把值传递给顶点着色器然后再传给片断着色器。让我们来画一个由两个不同颜色三角形组成的矩形。我们需要给顶点着色器添加一个属性值， 把值通过属性传递给它后它再直接传递给片断着色器。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">attribute vec2 a_position;</span><br><span class="line">attribute vec4 a_color;</span><br><span class="line">...</span><br><span class="line">varying vec4 v_color;</span><br><span class="line"> </span><br><span class="line">void main() &#123;</span><br><span class="line">...</span><br><span class="line">&#x2F;&#x2F; 直接把属性值中的数据赋给可变量</span><br><span class="line">v_color &#x3D; a_color;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>现在要给WebGL提供要用的颜色.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; look up where the vertex data needs to go.</span><br><span class="line">var positionLocation &#x3D; gl.getAttribLocation(program, &quot;a_position&quot;);</span><br><span class="line">var colorLocation &#x3D; gl.getAttribLocation(program, &quot;a_color&quot;);</span><br><span class="line">...</span><br><span class="line">&#x2F;&#x2F; Create a buffer for the colors.</span><br><span class="line">var buffer &#x3D; gl.createBuffer();</span><br><span class="line">gl.bindBuffer(gl.ARRAY_BUFFER, buffer);</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; Set the colors.</span><br><span class="line">setColors(gl);</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; setup attributes</span><br><span class="line">...</span><br><span class="line">&#x2F;&#x2F; tell the color attribute how to pull data out of the current ARRAY_BUFFER</span><br><span class="line">gl.enableVertexAttribArray(colorLocation);</span><br><span class="line">var size &#x3D; 4;</span><br><span class="line">var type &#x3D; gl.FLOAT;</span><br><span class="line">var normalize &#x3D; false;</span><br><span class="line">var stride &#x3D; 0;</span><br><span class="line">var offset &#x3D; 0;</span><br><span class="line">gl.vertexAttribPointer(colorLocation, size, type, normalize, stride, offset);</span><br><span class="line"> </span><br><span class="line">...</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; Fill the buffer with colors for the 2 triangles</span><br><span class="line">&#x2F;&#x2F; that make the rectangle.</span><br><span class="line">function setColors(gl) &#123;</span><br><span class="line">&#x2F;&#x2F; Pick 2 random colors.</span><br><span class="line">var r1 &#x3D; Math.random();</span><br><span class="line">var b1 &#x3D; Math.random();</span><br><span class="line">var g1 &#x3D; Math.random();</span><br><span class="line"> </span><br><span class="line">var r2 &#x3D; Math.random();</span><br><span class="line">var b2 &#x3D; Math.random();</span><br><span class="line">var g2 &#x3D; Math.random();</span><br><span class="line"> </span><br><span class="line">gl.bufferData(</span><br><span class="line">gl.ARRAY_BUFFER,</span><br><span class="line">new Float32Array(</span><br><span class="line">[ r1, b1, g1, 1,</span><br><span class="line">r1, b1, g1, 1,</span><br><span class="line">r1, b1, g1, 1,</span><br><span class="line">r2, b2, g2, 1,</span><br><span class="line">r2, b2, g2, 1,</span><br><span class="line">r2, b2, g2, 1]),</span><br><span class="line">gl.STATIC_DRAW);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里是结果。</p>
<p><a href="https://webglfundamentals.org/webgl/webgl-2d-rectangle-with-2-colors.html" target="_blank" rel="nofollow noopener" data-token="3fbc25234a4c212e9675efa83a8d85fc" style="box-sizing: border-box;outline: 0px;color: rgb(103, 149, 181);cursor: pointer;overflow-wrap: break-word;" data-linktype="2">点此在新窗口中浏览</a></p>
<p>你可能注意到这两个三角形是纯色的。我们传递给每个三角形的顶点的颜色值是相同的， 所以我们传递的varying会被插值成相同的颜色，如果我们传递不同的颜色，就会看到插值的颜色。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 给矩形的两个三角形</span><br><span class="line">&#x2F;&#x2F; 设置颜色值并发到缓冲</span><br><span class="line">function setColors(gl) &#123;</span><br><span class="line">&#x2F;&#x2F; 给每个顶点定义不同的颜色</span><br><span class="line">gl.bufferData(</span><br><span class="line">gl.ARRAY_BUFFER,</span><br><span class="line">new Float32Array(</span><br><span class="line">[ Math.random(), Math.random(), Math.random(), 1,</span><br><span class="line">Math.random(), Math.random(), Math.random(), 1,</span><br><span class="line">Math.random(), Math.random(), Math.random(), 1,</span><br><span class="line">Math.random(), Math.random(), Math.random(), 1,</span><br><span class="line">Math.random(), Math.random(), Math.random(), 1,</span><br><span class="line">Math.random(), Math.random(), Math.random(), 1]),</span><br><span class="line">gl.STATIC_DRAW);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>现在看到的是插值的varying。</p>
<p><a href="https://webglfundamentals.org/webgl/webgl-2d-rectangle-with-random-colors.html" target="_blank" rel="nofollow noopener" data-token="91879ff15f1603d766d03a2a7dee3bbd" style="box-sizing: border-box;outline: 0px;color: rgb(103, 149, 181);cursor: pointer;overflow-wrap: break-word;" data-linktype="2">点此在新窗口中浏览</a></p>
<p>可能不值一提的是上例还演示了使用多个属性并且通过顶点着色器向片断着色器传值。如果你看了<a href="https://webglfundamentals.org/webgl/lessons/zh_cn/webgl-image-processing.html" target="_blank" rel="nofollow noopener" data-token="270788592d118b44024af87238e132ea" style="box-sizing: border-box;outline: 0px;color: rgb(103, 149, 181);cursor: pointer;overflow-wrap: break-word;" data-linktype="2">处理图片的例子</a>， 那里面还用了另外一个属性传递纹理坐标。</p>
<p>关于<code>buffer</code>和<code>attribute</code>的代码是干什么的？</p>
<p>缓冲操作是在GPU上获取顶点和其他顶点数据的一种方式。<code>gl.createBuffer</code>创建一个缓冲；gl.bindBuffer是设置缓冲为当前使用缓冲；<code>gl.bufferData</code>将数据拷贝到缓冲，这个操作一般在初始化完成。</p>
<p>一旦数据存到缓冲中，还需要告诉WebGL怎么从缓冲中提取数据传给顶点着色器的属性。</p>
<p>要做这些，首先需要获取WebGL给属性分配的地址，如下方代码所示</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 询问顶点数据应该放在哪里</span><br><span class="line">var positionLocation &#x3D; gl.getAttribLocation(program, &quot;a_position&quot;);</span><br><span class="line">var colorLocation &#x3D; gl.getAttribLocation(program, &quot;a_color&quot;);</span><br></pre></td></tr></table></figure>
<p>这一步一般也是在初始化部分完成。</p>
<p>一旦知道了属性的地址，在绘制前还需要发出三个命令。</p>
<p><code>gl.enableVertexAttribArray(location);</code><br>这个命令是告诉WebGL我们想从缓冲中提供数据。</p>
<p><code>gl.bindBuffer(gl.ARRAY_BUFFER, someBuffer);</code><br>这个命令是将缓冲绑定到 ARRAY_BUFFER 绑定点，它是WebGL内部的一个全局变量。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">gl.vertexAttribPointer(</span><br><span class="line">location,</span><br><span class="line">numComponents,</span><br><span class="line">typeOfData,</span><br><span class="line">normalizeFlag,</span><br><span class="line">strideToNextPieceOfData,</span><br><span class="line">offsetIntoBuffer);</span><br></pre></td></tr></table></figure>

<p>这个命令告诉WebGL从 <code>ARRAY_BUFFER</code>绑定点当前绑定的缓冲获取数据。每个顶点有几个单位的数据(1 - 4)，单位数据类型是什么(BYTE, FLOAT, INT, UNSIGNED_SHORT, 等等…)， stride 是从一个数据到下一个数据要跳过多少位，最后是数据在缓冲的什么位置。</p>
<p>单位个数永远是 1 到 4 之间。</p>
<p>如果每个类型的数据都用一个缓冲存储，stride 和 offset 都是 0 。对 stride 来说 0 表示 “用符合单位类型和单位个数的大小”。对 offset 来说 0 表示从缓冲起始位置开始读取。它们使用 0 以外的值时会复杂得多，虽然这样会取得一些性能能上的优势， 但是一般情况下并不值得，除非你想充分压榨WebGL的性能。</p>
<p>希望这些关于缓冲和属性的内容对你来说讲的足够清楚。</p>
<p><code>vertexAttribPointer</code> 中的 <code>normalizeFlag</code> 参数是什么意思？</p>
<p>标准化标记（normalizeFlag）适用于所有非浮点型数据。如果传递false就解读原数据类型。BYTE 类型的范围是从 -128 到 127，UNSIGNED_BYTE 类型的范围是从 0 到 255， SHORT 类型的范围是从 -32768 到 32767，等等…</p>
<p>如果标准化标记设为true，BYTE 数据的值(-128 to 127)将会转换到 -1.0 到 +1.0 之间， UNSIGNED_BYTE (0 to 255) 变为 0.0 到 +1.0 之间，SHORT 也是转换到 -1.0 到 +1.0 之间， 但比 BYTE 精确度高。</p>
<p>最常用的是标准化颜色数据。大多数情况颜色值范围为 0.0 到 +1.0。使用4个浮点型数据存储红，绿，蓝和阿尔法通道数据时，每个顶点的颜色将会占用16字节空间， 如果你有复杂的几何体将会占用很多内存。代替的做法是将颜色数据转换为四个 UNSIGNED_BYTE ， 其中 0 表示 0.0，255 表示 1.0。现在每个顶点只需要四个字节存储颜色值，省了 75% 空间。</p>
<p>我们来修改之前代码实现。当我们告诉WebGL如何获取颜色数据时将这样</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 告诉颜色属性如何从colorBuffer中提取数据 (ARRAY_BUFFER)</span><br><span class="line">var size &#x3D; 4; &#x2F;&#x2F; 每次迭代使用四个单位数据</span><br><span class="line">var type &#x3D; gl.UNSIGNED_BYTE; &#x2F;&#x2F; 数据类型是8位的 UNSIGNED_BYTE 类型。</span><br><span class="line">var normalize &#x3D; true; &#x2F;&#x2F; 标准化数据</span><br><span class="line">var stride &#x3D; 0; &#x2F;&#x2F; 0 &#x3D; 移动距离 * 单位距离长度sizeof(type)</span><br><span class="line">&#x2F;&#x2F; 每次迭代跳多少距离到下一个数据</span><br><span class="line">var offset &#x3D; 0; &#x2F;&#x2F; 从缓冲的起始处开始</span><br><span class="line">gl.vertexAttribPointer(</span><br><span class="line">colorLocation, size, type, normalize, stride, offset)</span><br><span class="line">如下向缓冲添加数据</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 给矩形的两个三角形</span><br><span class="line">&#x2F;&#x2F; 设置颜色值并发到缓冲</span><br><span class="line">function setColors(gl) &#123;</span><br><span class="line">&#x2F;&#x2F; 设置两个随机颜色</span><br><span class="line">var r1 &#x3D; Math.random() * 256; &#x2F;&#x2F; 0 到 255.99999 之间</span><br><span class="line">var b1 &#x3D; Math.random() * 256; &#x2F;&#x2F; 这些数据</span><br><span class="line">var g1 &#x3D; Math.random() * 256; &#x2F;&#x2F; 在存入缓冲时</span><br><span class="line">var r2 &#x3D; Math.random() * 256; &#x2F;&#x2F; 将被截取成</span><br><span class="line">var b2 &#x3D; Math.random() * 256; &#x2F;&#x2F; Uint8Array 类型</span><br><span class="line">var g2 &#x3D; Math.random() * 256;</span><br><span class="line"> </span><br><span class="line">gl.bufferData(</span><br><span class="line">gl.ARRAY_BUFFER,</span><br><span class="line">new Uint8Array( &#x2F;&#x2F; Uint8Array</span><br><span class="line">[ r1, b1, g1, 255,</span><br><span class="line">r1, b1, g1, 255,</span><br><span class="line">r1, b1, g1, 255,</span><br><span class="line">r2, b2, g2, 255,</span><br><span class="line">r2, b2, g2, 255,</span><br><span class="line">r2, b2, g2, 255]),</span><br><span class="line">gl.STATIC_DRAW);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里是结果。</p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-04-27T04:50:46.480Z" itemprop="dateUpdated">2020-04-27 12:50:46</time>
</span><br>


        
        这里可以写作者留言，标签和 hexo 中所有变量及辅助函数等均可调用，示例：<a href="/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/" target="_blank" rel="external">http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/</a>
        
    </div>
    
    <footer>
        <a href="http://yoursite.com">
            <img src="/img/avatar.jpg" alt="Mr.Zhang">
            Mr.Zhang
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/WebGL/" rel="tag">WebGL</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/&title=《webGL|webgl2.0工作原理[转]》 — 一只咸鱼&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/&title=《webGL|webgl2.0工作原理[转]》 — 一只咸鱼&source=分享各种福利和姿势..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《webGL|webgl2.0工作原理[转]》 — 一只咸鱼&url=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2020/04/27/Vue-%E5%9F%BA%E4%BA%8EVue%E7%9A%84webGIS%E5%B1%95%E7%A4%BA/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Vue|基于Vue的webGIS展示</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2020/04/25/cesium-%E6%BA%90%E7%A0%81%E8%B7%9F%E8%AF%BB-%E6%95%B0%E6%8D%AE%E8%B0%83%E5%BA%A6/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">cesium|源码跟读 - cesium的数据调度渲染过程</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Mr.Zhang &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/&title=《webGL|webgl2.0工作原理[转]》 — 一只咸鱼&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/&title=《webGL|webgl2.0工作原理[转]》 — 一只咸鱼&source=分享各种福利和姿势..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《webGL|webgl2.0工作原理[转]》 — 一只咸鱼&url=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://yoursite.com/2020/04/27/webGL-webgl2-0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%BD%AC/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
